<?php

namespace App\Console;

use App\Models\Category;
use App\Models\CategoryStatistics;
use App\Models\Order;
use App\Repositories\CategoryStatisticsRepository;
use App\Repositories\DistributionStatisticsRepository;
use App\Repositories\OrderRepository;
use App\Repositories\StatisticsRepository;
use App\User;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
        $order_repostory = app()->build(OrderRepository::class);
        $schedule->call(function ()use($order_repostory) {
            $orders = Order::where([
                ['status',1],
                ['created_at', '<=', date('Y-m-d H:i:s',time()-15*60)],
            ])->get();
            foreach ($orders as $order) {
                $order_repostory->error($order,-1);
            }
        })->everyMinute();
        $statistics_repostory = app()->build(StatisticsRepository::class);
        $category_statistics_repostory = app()->build(CategoryStatisticsRepository::class);
        $distribution_statistics_repostory = app()->build(DistributionStatisticsRepository::class);

        $schedule->call(function ()use($statistics_repostory, $category_statistics_repostory, $distribution_statistics_repostory) {
            //统计
            $statistics_repostory->generate();

            //分类统计
            $categories = Category::all();
            foreach ($categories as $category) {
                $category_statistics_repostory->generate($category->id);
            }

            //配送统计
            $users = User::all();
            foreach ($users as $user) {
                if($user->is_distribution_statistics)
                    $distribution_statistics_repostory->generate($user);
            }

            //订单状态
            $order = Order::where([
                ['status',4],
                ['created_at','<',date('Y-m-d')],
            ])->orWhere([
                ['pairing_id','>',0],
                ['status',4],
                ['created_at','<',date('Y-m-d')],
            ])->update(['status'=>5]);

            Order::where('will_take',1)->update(['shipping_status'=>5]);
        })->dailyAt('00:01');

        $schedule->call(function ()use($statistics_repostory, $category_statistics_repostory) {
            $statistics_repostory->generate();

            $categories = Category::all();
            foreach ($categories as $category) {
                $category_statistics_repostory->generate($category->id);
            }
        })->dailyAt('17:01');

        $schedule->call(function ()use($statistics_repostory, $distribution_statistics_repostory) {
            $statistics_repostory->generate(30,3);
            //配送月统计
            $users = User::all();
            foreach ($users as $user) {
                if($user->is_distribution_statistics)
                    $distribution_statistics_repostory->generate($user,1,3);
            }
        })->monthlyOn(1, '00:10');
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
